package cn.innosmart.aq.customize;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.widget.CheckBox;

import cn.innosmart.aq.R;


/**
 * Created by Woody on 2015/4/9.
 */
public class CustomCheckBox extends CheckBox {
    public CustomCheckBox(Context context) {
        this(context, null);
    }

    public CustomCheckBox(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    private Rect mTextRect;
    private Paint mTextPaint;
    private Rect mLineRect;
    private Paint mLinePaint;
    private Bitmap mImage;
    private int mImageScale;
    private String mTitle;
    private int mTextOriginColor;
    private int mTextCheckedColor;
    private int mLineOriginColor;
    private int mLineCheckedColor;
    private int mTextSize;
    private boolean leftFirst=false;
    private boolean rightLast=false;
    private Paint mBgPaint;
    private TextPaint paint;

    public CustomCheckBox(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        /**
         * 获得我们所定义的自定义样式属性
         */
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomCheckBox, defStyle, 0);
        int n = a.getIndexCount();

        for (int i = 0; i < n; i++)
        {
            int attr = a.getIndex(i);

            switch (attr)
            {
                case R.styleable.CustomCheckBox_image:
                    mImage = BitmapFactory.decodeResource(getResources(), a.getResourceId(attr, 0));
                    break;
                case R.styleable.CustomCheckBox_imageScaleType:
                    mImageScale = a.getInt(attr, 0);
                    break;
                case R.styleable.CustomCheckBox_titleText:
                    mTitle = a.getString(attr);
                    break;
                case R.styleable.CustomCheckBox_titleTextSize:
                    mTextSize = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
                            16, getResources().getDisplayMetrics()));
                    break;
                case R.styleable.CustomCheckBox_textOriginColor:
                    mTextOriginColor=a.getColor(attr, Color.WHITE);
                    break;
                case R.styleable.CustomCheckBox_textCheckedColor:
                    mTextCheckedColor=a.getColor(attr, Color.GREEN);
                    break;
                case R.styleable.CustomCheckBox_leftFirst:
                    leftFirst=a.getBoolean(attr, false);
                    break;

            }
        }
        a.recycle();
        /**
         * 设置字体
         */
        //设置Text的位置
        mTextRect = new Rect();
        mTextPaint=new Paint();
        mTextPaint.setTextSize(mTextSize);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        mTextPaint.setAntiAlias(true);
        // 计算了描绘字体需要的范围
        mTextPaint.getTextBounds(mTitle, 0, mTitle.length(), mTextRect);
        /**
         * 设置字下面的下划线表示为选中状态
         */
        mLinePaint = new Paint();
        mLineRect = new Rect();
        mBgPaint=new Paint();
        mBgRect=new Rect();
    }
    private Rect mBgRect;
    @Override
    public boolean isChecked() {
        return super.isChecked();
    }
    private int mWidth;
    private int mHeight;
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        /**
         * 设置宽度
         */
        int specMode = MeasureSpec.getMode(widthMeasureSpec);
        int specSize = MeasureSpec.getSize(widthMeasureSpec);

        if (specMode == MeasureSpec.EXACTLY)// match_parent , accurate
        {
            mWidth = specSize;
        } else
        {
            // 由图片决定的宽
            int desireByImg = getPaddingLeft() + getPaddingRight() + mImage.getWidth();
            // 由字体决定的宽
            int desireByTitle = getPaddingLeft() + getPaddingRight() + mTextRect.width();

            if (specMode == MeasureSpec.AT_MOST)// wrap_content
            {
                int desire = desireByImg;//Math.max(desireByImg, desireByTitle);
                mWidth = Math.min(desire, specSize);
                Log.e("xxx", "AT_MOST");
            }
        }
        /***
         * 设置高度
         */

        specMode = MeasureSpec.getMode(heightMeasureSpec);
        specSize = MeasureSpec.getSize(heightMeasureSpec);
        if (specMode == MeasureSpec.EXACTLY)// match_parent , accurate
        {
            mHeight = specSize;
        } else
        {
            int desire = getPaddingTop() + getPaddingBottom() + mImage.getHeight();
            if (specMode == MeasureSpec.AT_MOST)// wrap_content
            {
                mHeight = desire;
            }
        }
        setMeasuredDimension(mWidth, mHeight);
    }
    private boolean onTouch=false;
    @Override
    protected void onDraw(final Canvas canvas) {
        super.onDraw(canvas);
        //左边第一个 要给左边加上圆角

        //右边最后一个  要给右边加上圆角
        //正常的绘制  true时的绘制
        if(super.isChecked()){
            //先画背景？

            mBgRect.left = 0;
            mBgRect.right = mWidth;
            mBgRect.top = 0;
            mBgRect.bottom = mHeight;
            if(onTouch){
                ColorMatrix colorMatrix = new ColorMatrix();
                float[] colorArray = {1,0,0,0,0,0,3,0,0,0,0,0,1,0,0,0,0,0,1,0};
                colorMatrix.setSaturation(0);
                ColorMatrixColorFilter colorMatrixFilter = new ColorMatrixColorFilter(colorMatrix);
                mBgPaint.setColorFilter(colorMatrixFilter);
            }else{
                ColorMatrix colorMatrix = new ColorMatrix();
                float[] colorArray = {1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0};
               // colorMatrix.setSaturation(0);
                ColorMatrixColorFilter colorMatrixFilter = new ColorMatrixColorFilter(colorMatrix);
                mBgPaint.setColorFilter(colorMatrixFilter);
            }
            canvas.drawBitmap(mImage, null, mBgRect,mBgPaint);
            //在画文字
            // 创建画笔
            //正常情况，将字体居中
            if(leftFirst==true){
                mTextPaint.setColor(Color.RED);
            }else{
                mTextPaint.setColor(Color.parseColor("#FF7ed317"));
            }
            canvas.drawText(mTitle, mWidth/2 ,(mHeight/2+mTextRect.width()/2-0.13f*mHeight),mTextPaint);
            //再画下面的线
            mLinePaint.setColor(Color.parseColor("#FF7ed317"));
            canvas.drawRect(mWidth/10,0.8f*mHeight, 0.9f*mWidth, 0.86f*mHeight,mLinePaint);// 长方形
        }else{
            //false时的绘制
            //先画背景？
            mBgRect.left = 0;
            mBgRect.right = mWidth;
            mBgRect.top = 0;
            mBgRect.bottom = mHeight;
            if(onTouch){
                ColorMatrix colorMatrix = new ColorMatrix();
                float[] colorArray = {1,0,0,0,0,0,3,0,0,0,0,0,1,0,0,0,0,0,1,0};
               colorMatrix.setSaturation(0);
                ColorMatrixColorFilter colorMatrixFilter = new ColorMatrixColorFilter(colorMatrix);
                mBgPaint.setColorFilter(colorMatrixFilter);
            }else{
                ColorMatrix colorMatrix = new ColorMatrix();
                float[] colorArray = {1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0};
                //colorMatrix.setSaturation(0);
                ColorMatrixColorFilter colorMatrixFilter = new ColorMatrixColorFilter(colorMatrix);
                mBgPaint.setColorFilter(colorMatrixFilter);
            }
            canvas.drawBitmap(mImage, null, mBgRect,mBgPaint);
            //在画文字
            // 创建画笔
            if(leftFirst==true){
                mTextPaint.setColor(Color.RED);
            }else{
                mTextPaint.setColor(Color.WHITE);
            }
             canvas.drawText(mTitle, mWidth/2 ,(mHeight/2+mTextRect.width()/2-0.13f*mHeight),mTextPaint);
            //再画下面的线
            mLinePaint.setColor(Color.WHITE);
            canvas.drawRect(mWidth/10,0.8f*mHeight, 0.9f*mWidth, 0.86f*mHeight,mLinePaint);// 长方形
        }
        this.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if(motionEvent.getAction()== MotionEvent.ACTION_DOWN){
                    onTouch=true;
                }else if(motionEvent.getAction()== MotionEvent.ACTION_UP){
                    onTouch=false;
                }
                postInvalidate();
                return false;
            }
        });

        //点击事件
        this.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //postInvalidate();
            }
        });
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);
    }
}
